package com.example.mask.Class;

import com.example.mask.ResourceTable;

import com.example.rpxutil.Class.RpxUtil;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.PropertyKey;
import ohos.multimodalinput.event.KeyEvent;
import ohos.multimodalinput.event.TouchEvent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.SerializablePermission;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CustomLoading extends CommonDialog {
    public CustomLoading(Context context) {
        super(context);
        setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        setTransparent(true);
        initComponent(context);
    }

    @Override
    protected void create() {
        super.create();
    }
    public void initComponent(Context context)  {
        Image image = new Image(context);
//        FrameAnimationElement frameAnimationElement = new FrameAnimationElement(context, ResourceTable.Graphic_loading_animation);
//        Component component = new Component(context);
//        component.setWidth(300);
//        component.setHeight(500);
//        component.setBackground(frameAnimationElement);
//        frameAnimationElement.start();
        DirectionalLayout directionalLayout = new DirectionalLayout(context);
        DirectionalLayout directionalLayout2 = new DirectionalLayout(context);
         image.setScaleMode(Image.ScaleMode.INSIDE);
        List<Integer> getid = getid();
        Integer integer = getid.get(3);
        image.setPixelMap(integer);
        Text text = new Text(context);
        text.setText("加载中......");
        text.setTextSize((int) RpxUtil.rpx(60,context));
        text.setTextColor(Color.WHITE);
        directionalLayout2.addComponent(image);

        directionalLayout.addComponent(text);
        image.setPixelMap(ResourceTable.Media_gifloading1);//初始化

//        start(context,directionalLayout2);
        directionalLayout.addComponent(directionalLayout2);
        setContentCustomComponent(directionalLayout);
    }
    private void start(Context context,DirectionalLayout directionalLayout) {
        List<Integer> getid = getid();
        Image image1 = new Image(context);
        image1.setScaleMode(Image.ScaleMode.INSIDE);
        image1.setPixelMap(ResourceTable.Media_gifloading1);
        directionalLayout.addComponent(image1);
        context.createSerialTaskDispatcher("", TaskPriority.DEFAULT).asyncDispatch(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        for (int i =0; i < getid.size() ; i++) {
                            Thread.sleep(1*1000L);
                            int  index=i;
                        context.getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
//                                directionalLayout.removeAllComponents();
                                directionalLayout.removeComponentAt(0);
                                Image image1 = new Image(context);
                                image1.setScaleMode(Image.ScaleMode.INSIDE);
                                image1.setPixelMap(getid.get(index));
                                System.out.println("getid.get(index)"+getid.get(index)+index);
                                directionalLayout.addComponent(image1);
                            }
                        });
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    public List<Integer> getid(){
        List<Integer> list = new ArrayList<>();
        list.add(ResourceTable.Media_gifloading1);
        list.add(ResourceTable.Media_gifloading2);
        list.add(ResourceTable.Media_gifloading3);
        list.add(ResourceTable.Media_gifloading4);
        list.add(ResourceTable.Media_gifloading5);
        list.add(ResourceTable.Media_gifloading6);
        list.add(ResourceTable.Media_gifloading7);
        list.add(ResourceTable.Media_gifloading8);
        list.add(ResourceTable.Media_gifloading9);
        return list;
    }
    @Override
    protected void onWindowConfigUpdated(WindowManager.LayoutConfig configParam) {
        super.onWindowConfigUpdated(configParam);
    }

    @Override
    protected void onWindowSelectionUpdated(boolean isSelectionUpdated) {
        super.onWindowSelectionUpdated(isSelectionUpdated);
    }

    @Override
    public boolean isMovable() {
        return super.isMovable();
    }

    @Override
    public void setMovable(boolean movable) {
        super.setMovable(movable);
    }

    @Override
    public Rect getBoundRect() {
        return super.getBoundRect();
    }

    @Override
    public void setBoundRect(Rect boundRect) {
        super.setBoundRect(boundRect);
    }

    @Override
    public CommonDialog setTitleIcon(int resId, int iconId) {
        return super.setTitleIcon(resId, iconId);
    }

    @Override
    public CommonDialog setTitleText(String text) {
        return super.setTitleText(text);
    }

    @Override
    public CommonDialog setTitleSubText(String text) {
        return super.setTitleSubText(text);
    }

    @Override
    public CommonDialog setTitleCustomComponent(DirectionalLayout component) {
        return super.setTitleCustomComponent(component);
    }

    @Override
    public CommonDialog setContentImage(int resId) {
        return super.setContentImage(resId);
    }

    @Override
    public CommonDialog setContentText(String text) {
        return super.setContentText(text);
    }

    @Override
    public CommonDialog setContentCustomComponent(Component component) {
        return super.setContentCustomComponent(component);
    }

    @Override
    public Component getContentCustomComponent() {
        return super.getContentCustomComponent();
    }

    @Override
    public Component getButtonComponent() {
        return super.getButtonComponent();
    }

    @Override
    public Window getWindow() {
        return super.getWindow();
    }

    @Override
    public CommonDialog setButton(int buttonNum, String text, ClickedListener listener) {
        return super.setButton(buttonNum, text, listener);
    }

    @Override
    public CommonDialog setImageButton(int buttonNum, int resId, ClickedListener listener) {
        return super.setImageButton(buttonNum, resId, listener);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setDestroyedListener(DestroyedListener destroyedListener) {
        super.setDestroyedListener(destroyedListener);
    }

    @Override
    public boolean setSwipeToDismiss(boolean isEnabled) {
        return super.setSwipeToDismiss(isEnabled);
    }

    @Override
    public boolean deliverKeyboardCase(KeyEvent event) {
        return super.deliverKeyboardCase(event);
    }

    @Override
    public boolean clickKeyDown(KeyEvent event) {
        return super.clickKeyDown(event);
    }

    @Override
    public boolean clickKeyUp(KeyEvent event) {
        return super.clickKeyUp(event);
    }

    @Override
    public boolean deliverTouchCase(TouchEvent event) {
        return super.deliverTouchCase(event);
    }

    @Override
    public boolean dealTouchEvent(TouchEvent event) {
        return super.dealTouchEvent(event);
    }

    @Override
    public void siteKeyboardCallback(KeyboardCallback keyboardCallback) {
        super.siteKeyboardCallback(keyboardCallback);
    }

    @Override
    public void siteRemovable(boolean removable) {
        super.siteRemovable(removable);
    }

    @Override
    public void dealBackKeyDown() {
        super.dealBackKeyDown();
    }
}
